[TOC]

While-loop

While-loops are usually used when you want to repeat a set of operations until some conditions are met. In this situation, you may not know how many times it will take to fulfil these conditions.

Let's consider an example. We want to demonstrate that as , where

But, we don't know how large should be to make the error small enough. Hence, we use a while loop with the termination condition . The while-loop will exit when this condition is met. This can be done in the following:

Input
s = 0;
i = 1;
% Arbitrarily large number, so that
% it can enter the while-loop.
err = 100; 
% Tolerance.
tol = 1e-6;
while err > tol
		s = s + 1/2^i;
		i = i + 1;
		err = abs(s - 1);
end
i	% final i value
err
Output
i = 
 21.00000000000000

err = 1e-7 × 
 9.536743164062500

The above output shows that it took 21 iterations to fulfil the termination condition, which resulted in the error equal to approximately (less than the tolerance ).

Syntax

A while-loop takes the following general form:

while logical_value
		statement_1
		statement_2
		...
		statement_n
end

The loop continues until logical_value is false. When a stopping condition is met, this logical_value should evaluate to false and hence the loop terminates. In the example above, the logical_value was given by evaluating the condition err > tol.

One-line Form

The general syntax shown here is the most commonly used form. Nevertheless, a while-loop can also be written in one single line with separators , or ;. Both of the examples below give the same a as the final results.

Input
disp('Example 1')
i = 1;
a = 0;
while a < 5, a = a + i, i = i + 1, end
a

disp('Example 2')
i = 1;
a = 0;
while a < 5; a = a + i; i = i + 1; end
a
Output
Example 1
a = 
 1.0000

i = 
 2.0000

a = 
 3.0000

i = 
 3.0000

a = 
 6.0000

i = 
 4.0000

a = 
 6.0000

Example 2
a = 
 6.0000

In the above example, different components of the while-loop are separated by separators. A separator can be a comma, a semicolon, or a newline character.

⚠️⚠️⚠️ The end keyword should be followed by a newline character. This is different from MATLAB in which the end can be followed by other separators.

Infinite Loop

⚠️⚠️⚠️ If logical_value is always true (e.g., using while true), the while-loop will loop indefinitely without showing any warning or error messages. In this situation, the app may take up so much memory that the user-interface may become unresponsive (the app may even crash). If this happens, try to terminate the execution by tapping the stop button 􀛷 on the top toolbar of the Console. If this doesn't work, try to quite the app entirely.

For example, running the following code will result in infinite looping.

a = 0
while true
	a = a + 1;
end

Loop Within Loops

A while-loop can be embedded within another while-loop, as shown in the example below.

Input
sum_all_boxes = 0;
while sum_all_boxes <= 2000
    sum_one_box = 0;
    while sum_one_box <= 100
        sum_one_box = sum_one_box + rand;
    end
    sum_all_boxes = sum_all_boxes + sum_one_box;
end
sum_all_boxes
Output
sum_all_boxes = 
 2006.353925543614

What About Do-while?

There is no do-while loop in both MATLAB and this app. Nevertheless, one can construct a loop similar to do-while by using the keyword break as shown in the example below. It increments a and stops when a is equal to 10. Note here that a is incremented before checking the condition a >= 10. Therefore, it performs like a do-while loop.

Input
a = 0;
while true
	a = a + 1;
	if a >= 10
		break
	end
end
a
Output
a = 
 10.000

More Examples

Input
clear
a = 0;
count = 0;
while a < 10
    a = a + rand();
    count=count + 1;
end 
count
Output
All variables cleared
count = 
 21.000
Input
clear
% Initial amount of money
money = 100;
day = 1;
while money >= day
		% Donate money.
    money = money - day;
    day = day + 1;
end 
total_donation = 100 - money
Output
All variables cleared
total_donation = 
 91.000